﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using Public;

namespace QuanLyQuanCaPhe
{
    public partial class NhanVien : DevExpress.XtraEditors.XtraForm
    {
        NhanVien_BUS nv_bus = new NhanVien_BUS();
        NhanVien_PB nv_pb=new NhanVien_PB();
        int flag = 0;

        public NhanVien()
        {
            InitializeComponent();
        }

        private void labelControl1_Click(object sender, EventArgs e)
        {

        }

        private void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void groupControl1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void textEdit2_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void NhanVien_Load(object sender, EventArgs e)
        {
            //load cbo giới tính
            cboGioiTinh.Items.Clear();
            cboGioiTinh.Items.Add("Nam");
            cboGioiTinh.Items.Add("Nữ");
            cboGioiTinh.SelectedItem = "Nam";
            //load cbo mã ca
            cboMaCa.Properties.DataSource = nv_bus.LayDSMaCa();
            cboMaCa.Properties.DisplayMember = "tenca";
            cboMaCa.Properties.ValueMember = "maca";
            //load gridview
            gridNhanVien.DataSource = nv_bus.LayDSNhanVien();

            EnableEditing(true);
           
        }

        private void lookUpEdit1_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click_1(object sender, EventArgs e)
        {

        }
        #region "Lấy giá trị"
        private NhanVien_PB GetValues()
        {
            NhanVien_PB.MaNV = txtMa.Text;
            NhanVien_PB.Ten = txtHoTen.Text;
            NhanVien_PB.NgaySinh = Convert.ToDateTime(dtNgaySinh.EditValue);
            NhanVien_PB.GioiTinh = cboGioiTinh.SelectedItem.ToString();
            NhanVien_PB.CMND = txtCMND.Text;
            NhanVien_PB.DiaChi = txtDiaChi.Text;
            NhanVien_PB.SDT = txtSDT.Text;
            NhanVien_PB.MaCa = cboMaCa.EditValue.ToString();
            return nv_pb;
        }
        #endregion

        #region "Reset Text"
        private void ResetValues()
        {
            txtMa.ResetText();
            txtHoTen.ResetText();
            dtNgaySinh.ResetText();
            txtCMND.ResetText();
            txtSDT.ResetText();
            txtDiaChi.ResetText();
        }
        #endregion

        #region "Kiểm tra rỗng"
        private bool CheckNull()
        {
            if (txtMa.Text == "" || txtHoTen.Text == "" || cboGioiTinh.SelectedItem.ToString() == "" || dtNgaySinh.EditValue.ToString() == "" || txtCMND.Text == "" || cboMaCa.EditValue.ToString() == "")
                return false;
            return true;
        }
        #endregion

        #region "Enable control"
        private void EnableEditing(bool editing)
        {
            txtMa.Properties.ReadOnly = editing;
            txtHoTen.Properties.ReadOnly = editing;
            cboGioiTinh.Enabled = !editing;
            dtNgaySinh.Properties.ReadOnly = editing;
            txtCMND.Properties.ReadOnly = editing;
            txtDiaChi.Properties.ReadOnly = editing;
            cboMaCa.Properties.ReadOnly = editing;
            txtSDT.Properties.ReadOnly = editing;

            btnThem.Enabled = editing;
            btnSua.Enabled = editing;
            btnXoa.Enabled = editing;
            btnLuu.Enabled = !editing;
            btnHuy.Enabled = !editing;
            btnCapNhat.Enabled = editing;
            btnDong.Enabled = editing;
            //btnGiaiQuyetThoiViec.Enabled = editing;

            gridNhanVien.Enabled = editing;
        }
        #endregion

        private void btnThem_Click(object sender, EventArgs e)
        {
            flag = 1;
            EnableEditing(false);
            ResetValues();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (CheckNull())
            {
                GetValues();
                if (flag == 1)
                {
                    nv_bus.Them(nv_pb);
                    MessageBox.Show("Thêm thành công NV <"+txtHoTen.Text+"> vào dữ liệu", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (flag == 2)
                {
                    nv_bus.Sua(nv_pb);
                    MessageBox.Show("Sửa thông tin NV <" + txtHoTen.Text + "> thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                NhanVien_Load(sender, e);
            }
            else
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin","Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
        }

        private void txtMa_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void cboGioiTinh_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void txtSDT_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            txtMa.Text = gridView1.GetRowCellValue(e.FocusedRowHandle, "manv").ToString();
            txtHoTen.Text = gridView1.GetRowCellValue(e.FocusedRowHandle, "ten").ToString();
            cboGioiTinh.SelectedItem = gridView1.GetRowCellValue(e.FocusedRowHandle, "gioitinh").ToString();
            dtNgaySinh.EditValue = gridView1.GetRowCellValue(e.FocusedRowHandle, "ngaysinh").ToString();
            txtCMND.Text = gridView1.GetRowCellValue(e.FocusedRowHandle, "cmnd").ToString();
            txtSDT.Text = gridView1.GetRowCellValue(e.FocusedRowHandle, "sdt").ToString();
            cboMaCa.EditValue = gridView1.GetRowCellValue(e.FocusedRowHandle, "maca").ToString();
            txtDiaChi.Text = gridView1.GetRowCellValue(e.FocusedRowHandle, "diachi").ToString();
            string thoiviec = gridView1.GetRowCellValue(e.FocusedRowHandle, "trangthai").ToString();
            if (thoiviec == "True")
                btnGiaiQuyetThoiViec.Enabled = true;
            else
                btnGiaiQuyetThoiViec.Enabled = false;
        }

        private void gridView1_Click(object sender, EventArgs e)
        {
           

        }

        private void Sửa_Click(object sender, EventArgs e)
        {
            flag = 2;
            EnableEditing(false);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn xóa nhân viên <" + txtHoTen.Text + "> hay không", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                nv_bus.Xoa(txtMa.Text);
                gridNhanVien.DataSource = nv_bus.LayDSNhanVien();
            }
        }

        #region "Kiểm tra chỉ chấp nhận nhập số"
        private void txtCMND_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtSDT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        #endregion

        private void btnGiaiQuyetThoiViec_Click(object sender, EventArgs e)
        {
            GetValues();
            if (MessageBox.Show("Chấp nhận giải quyết cho nhân viên <" + txtHoTen.Text + "> nghỉ việc", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                nv_bus.GiaiQuyetThoiViec(nv_pb);
                gridNhanVien.DataSource = nv_bus.LayDSNhanVien();
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            NhanVien_Load(sender, e);
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}